import java.util.Stack;

/**
 * @program: LeetCode
 * @ClassName Num031_
 * @description: LeetCode : 剑指 Offer 31. 栈的压入、弹出序列
 * @author: WXY
 * @create: 2022-12-09 13:26
 * @Version 1.0
 **/

public class Num031_validateStackSequences {
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (int j = 0; j < pushed.length; j++) {
            stack.push(pushed[j]);
            while (!stack.empty() && stack.peek() == popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }
}
